﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Convertor.Interface;

namespace Convertor
{
    /// <summary>
    /// Log binding model
    /// </summary>
    public class LogViewModel : INotifyPropertyChanged
    {
        private List<LogContent> _list = new List<LogContent>();
        private IEnumerable<LogContent> _tempList = new List<LogContent>();
        /// <summary>
        /// 
        /// </summary>
        public IEnumerable<LogContent> LogList
        {
            get
            {
                if (string.IsNullOrEmpty(filter))
                {
                    _tempList = _list;
                    var a = _tempList.Skip((page - 1) * pageSize).Take(pageSize);
                    return a;
                }
                else
                {
                    Regex reg = null;
                    try
                    {
                        reg = new Regex(filter, RegexOptions.IgnoreCase);
                    }
                    catch { }
                    if (reg == null)
                    {
                        _tempList = _list.Where(p => p.PluginName.Contains(filter) || p.Log.Contains(filter));
                    }
                    else
                    {
                        _tempList = _list.Where(p => reg.IsMatch(p.PluginName) || reg.IsMatch(p.Log));

                    }
                    var a = _tempList.Skip((page - 1) * pageSize).Take(pageSize);
                    return a;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public int TotalCount { get { return _tempList == null ? 0 : _tempList.Count(); } }
        private int page = 1;
        /// <summary>
        /// 
        /// </summary>
        public int Page
        {
            get { return page; }
            set
            {
                page = Math.Max(1, Math.Min(PageCount, value));
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("LogList"));
                    PropertyChanged(this, new PropertyChangedEventArgs("Page"));
                }
            }
        }
        private int pageSize = 50;
        /// <summary>
        /// 
        /// </summary>
        public int PageSize
        {
            get { return pageSize; }
            set
            {
                pageSize = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("LogList"));
                    PropertyChanged(this, new PropertyChangedEventArgs("PageCount"));
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public int PageCount { get { return (int)Math.Ceiling((double)TotalCount / (double)PageSize); } }
        private string filter;
        /// <summary>
        /// 
        /// </summary>
        public string Filter
        {
            get { return filter; }
            set
            {
                filter = value;
                Update(true);
            }
        }

        private int lastTotalCount;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="restrictRefresh"></param>
        public void Update(bool restrictRefresh = false)
        {
            if (restrictRefresh || (PropertyChanged != null) && (lastTotalCount != TotalCount) && (Math.Ceiling((double)TotalCount / PageSize) == page))
            {
                PropertyChanged(this, new PropertyChangedEventArgs("LogList"));
                PropertyChanged(this, new PropertyChangedEventArgs("TotalCount"));
                PropertyChanged(this, new PropertyChangedEventArgs("PageCount"));
                lastTotalCount = TotalCount;
            }
            if (page > PageCount)
            {
                Page = PageCount;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="log"></param>
        public void Add(LogContent log)
        {
            this._list.Add(log);
            if (PropertyChanged != null)
            {
                //PropertyChanged(this, new PropertyChangedEventArgs("PageCount"));
                //PropertyChanged(this, new PropertyChangedEventArgs("TotalCount"));
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public void Clear()
        {
            Page = 1;
            this._list.Clear();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (var a in this._list)
            {
                sb.Append(a.PluginName);
                sb.Append("\t");
                sb.Append(a.Date);
                sb.Append("\t");
                sb.Append(a.Log);
                sb.Append("\r\n");
            }
            return sb.ToString();
        }


        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
